<%--
  Created by IntelliJ IDEA.
  User: 霞
  Date: 2024/9/27
  Time: 22:07
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.sql.*" %>  <!-- 导入 SQL 包 -->
<html>
<head>
    <title>修改结果</title>
</head>
<body>
<%
    String dbName = "school"; // 数据库名
    String userName = "root"; // 账户名
    String userPassword = "qwert"; // 密码
    String url = "jdbc:mysql://localhost:3306/" + dbName + "?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC"; // URL

    Class.forName("com.mysql.cj.jdbc.Driver"); // 5.7 及以上版本驱动器
    Connection conn = DriverManager.getConnection(url, userName, userPassword); // 连接数据库

    String id = request.getParameter("id"); // 获取要删除的学生学号
    String sql = "DELETE FROM student WHERE number = ?"; // SQL 删除命令
    PreparedStatement preparedStatement = conn.prepareStatement(sql); // 创建操作对象
    preparedStatement.setString(1, id); // 设置参数

    int result = preparedStatement.executeUpdate(); // 执行更新操作
    if (result > 0) {
        out.print("<script type='text/javascript'>alert('删除成功');</script>");
    } else {
        out.print("<script type='text/javascript'>alert('删除失败');</script>"); // 修改为“删除失败”
    }

    sql = "SELECT * FROM student"; // 查询所有学生信息
    ResultSet rs = preparedStatement.executeQuery(sql);
%>
<table border="1">
    <tr style="text-align: center">
        <th colspan="4" style="font-size: x-large;">学生表信息</th>
    </tr>
    <tr style="text-align: center">
        <td>学号</td>
        <td>姓名</td>
        <td>出生日期</td>
        <td>所在班级</td>
    </tr>
    <% while (rs.next()) { %>
    <tr style="text-align: center">
        <td><%= rs.getString(1) %></td>
        <td><%= rs.getString(2) %></td>
        <td><%= rs.getString("birthday") %></td>
        <td><%= rs.getString("stuclass") %></td>
    </tr>
    <% } %>
    <%
        rs.close();
        preparedStatement.close();
        conn.close();
    %>
</table>
</body>
</html>
